package com.study4j.common.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.study4j.common.enums.EnumSysCode;
import com.study4j.common.enums.IEnumCode;

/**
 * @description 后端响应结果封装（map机构）
 * @author jiangbin
 * @date 2018年8月7日 下午4:48:15
 * @version 1.0.0
 */
public class ResultMap extends HashMap<String, Object> implements Serializable {
    
    private static final long serialVersionUID = 8763953349107737932L;

    public ResultMap() {
        put("code", EnumSysCode.SUCCESS.getCode());
        put("msg", EnumSysCode.SUCCESS.getMessage());
    }

    public static ResultMap error() {
        return error(EnumSysCode.ERROR, EnumSysCode.ERROR.getMessage());
    }

    public static ResultMap error(String msg) {
        return error(EnumSysCode.ERROR, msg);
    }

    public static ResultMap error(IEnumCode code) {
        return error(code, code.getMessage());
    }

    public static ResultMap error(IEnumCode code, String msg) {
        ResultMap result = new ResultMap();
        result.put("code", code.getCode());
        result.put("msg", msg);
        return result;
    }

    public static ResultMap ok(String msg) {
        ResultMap result = new ResultMap();
        result.put("msg", msg);
        return result;
    }

    public static ResultMap ok(Map<String, Object> map) {
        ResultMap result = new ResultMap();
        result.putAll(map);
        return result;
    }

    public static ResultMap ok() {
        return new ResultMap();
    }

    @Override
    public ResultMap put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
